package com.xzx.study;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xzx.study.bean.ShopInfoV2;
import com.xzx.study.util.ImageLoader;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class ImageLoaderActivity extends AppCompatActivity {

    private static final String TAG = "TAG-" + ImageLoaderActivity.class.getSimpleName();
    private static final int REQUEST_COMPLETE = 1;

    private ListView listView;
    private LinearLayout linearLayout;

    private List<ShopInfoV2> data;
    private BaseAdapter baseAdapter;
    private ImageLoader imageLoader;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REQUEST_COMPLETE:
                    linearLayout.setVisibility(View.GONE);
                    listView.setVisibility(View.VISIBLE);
                    listView.setAdapter(baseAdapter);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_loader);

        listView = findViewById(R.id.list_view);
        linearLayout = findViewById(R.id.list_loading);

        imageLoader = new ImageLoader(this);
        baseAdapter = new ShopInfoV2Adapter();
        initData();
    }

    class ShopInfoV2Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = View.inflate(ImageLoaderActivity.this, R.layout.item_list_view_image_loader, null);
                holder = new ViewHolder();
                ImageView imageView = convertView.findViewById(R.id.image_loader_img);
                TextView name = convertView.findViewById(R.id.image_loader_name);
                TextView price = convertView.findViewById(R.id.image_loader_price);
                holder.imageView = imageView;
                holder.name = name;
                holder.price = price;
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ShopInfoV2 shopInfoV2 = data.get(position);
            imageLoader.loadImage(shopInfoV2.getImagePath(), holder.imageView);
            holder.name.setText(shopInfoV2.getName());
            holder.price.setText(shopInfoV2.getPrice() + "元");

//            ImageView imageView = convertView.findViewById(R.id.image_loader_img);
//            TextView name = convertView.findViewById(R.id.image_loader_name);
//            TextView price = convertView.findViewById(R.id.image_loader_price);

//            ShopInfoV2 shopInfoV2 = data.get(position);
//            imageLoader.loadImage(shopInfoV2.getImagePath(), imageView);
//            name.setText(shopInfoV2.getName());
//            price.setText(shopInfoV2.getPrice() + "元");

            return convertView;
        }
        class ViewHolder {
            ImageView imageView;
            TextView name;
            TextView price;
        }
    }

    private void initData() {
        new Thread(() -> {
            URL url = null;
            try {
                url = new URL("http://192.168.31.60:8182/android/shops");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setReadTimeout(5000);
                connection.setConnectTimeout(5000);
                connection.setRequestMethod("GET");
                connection.connect();
                int responseCode = connection.getResponseCode();
                if (responseCode == 200) {
                    Log.i(TAG, "请求成功");
                    InputStream inputStream = connection.getInputStream();
                    byte[] b = new byte[1024 * 1024];
                    int len = -1;
                    StringBuilder builder = new StringBuilder();
                    while ((len = inputStream.read(b)) != -1) {
                        builder.append(new String(b, 0, len));
                    }
                    Log.i(TAG, "请求结果：" + builder.toString());
                    data = new Gson().fromJson(builder.toString(), new TypeToken<List<ShopInfoV2>>() {
                    }.getType());
                    Log.i(TAG, "请求结果data：" + data);
                    inputStream.close();
                    connection.disconnect();
                    handler.sendEmptyMessage(REQUEST_COMPLETE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
}
